package com.quick.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author jojo.wang
 * @title: SlaveDataSourceConfig
 * @projectName spring-boot-quick
 * @description: TODO
 * @date 2019/5/515:05
 */
@Configuration
@EnableConfigurationProperties
@Slf4j
public class SlaveDataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.slave")
    public DataSourceProperties slaveDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    public DataSource slaveDataSource(){
        DataSourceProperties dataSourceProperties = slaveDataSourceProperties();
        log.info("slaveDataSource is {}",dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager slavePlatformTransactionManager(DataSource slaveDataSource){
        return new DataSourceTransactionManager(slaveDataSource);
    }
}
